package com.wyvoid.forum.web.filter;

import com.wyvoid.core.util.CookieUtils;
import com.wyvoid.core.util.DesCryptUtil;
import com.wyvoid.forum.Constant;
import com.wyvoid.forum.web.MyActionBeanContext;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.domain.SessionUser;
import com.wyvoid.forum.exception.user.PasswordFailureException;
import com.wyvoid.forum.exception.user.UsernameNotFoundException;
import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.util.ForumUtil;
import net.sourceforge.stripes.util.Log;
import org.apache.commons.lang.StringUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-12
 * Time: 7:37:25
 */
public class AutoLoginFilter implements javax.servlet.Filter {
    Log log = Log.getInstance(getClass());

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        MyActionBeanContext myActionBeanContext = new MyActionBeanContext(request, response);
        if (!myActionBeanContext.isUserLoggedIn()) {
            Cookie cookie = CookieUtils.getCookie(request, Constant.cookieName);
            if (cookie != null) {
                String value = cookie.getValue();
                try {
                    value = DesCryptUtil.dectrypt(value, (String) ForumUtil.settings.get("cookiekey"));
                    String[] values = StringUtils.split(value, "|");
                    String username = values[0];
                    String password = values[1];
                    String online = values[2];
                    IUserService userService = (IUserService) ForumUtil.context.getBean("userService");
                    User user = userService.loginUser(username, password);

                    SessionUser sessionUser = new SessionUser(user.getId(), user.getNickname(),Integer.parseInt(online));
                    sessionUser.setLastLoginTime(user.getLastLoginTime());

                    user.setLastLoginIp(request.getRemoteAddr());
                    user.setLastLoginTime(new Date());
                    userService.updateUser(user);

                    myActionBeanContext.setUser(sessionUser);
                    //request.getSession().setAttribute("user", user);

                } catch (PasswordFailureException e) {
                    log.warn("login failure because password error");
                } catch (UsernameNotFoundException e) {
                    log.warn("login failure because username not exists");
                } catch (Exception e) {
                    log.warn("cookie des failure");
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }

}
